package com.hnu.highlynewapp.service.impl;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.entity.UserEntity;
import com.hnu.highlynewapp.enums.StatusEnum;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.service.UserService;
import com.hnu.highlynewapp.vo.FinancialInstSearchVo;
import com.hnu.highlynewapp.vo.FinancialInstitutionInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.Query;

import com.hnu.highlynewapp.dao.FinancialInstitutionInfoDao;
import com.hnu.highlynewapp.entity.FinancialInstitutionInfoEntity;
import com.hnu.highlynewapp.service.FinancialInstitutionInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("financialInstitutionInfoService")
public class FinancialInstitutionInfoServiceImpl extends ServiceImpl<FinancialInstitutionInfoDao, FinancialInstitutionInfoEntity> implements FinancialInstitutionInfoService {

    @Resource
    private UserService userService;

    @Resource
    private ContactService contactService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FinancialInstitutionInfoEntity> page = this.page(
                new Query<FinancialInstitutionInfoEntity>().getPage(params),
                new QueryWrapper<FinancialInstitutionInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FinancialInstitutionInfoEntity> getListByParma(FinancialInstSearchVo vo) {

        QueryWrapper<FinancialInstitutionInfoEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(vo.getInvestField())) {
            wrapper.eq("invest_field", vo.getInvestField());
        }
        if (!StringUtils.isEmpty(vo.getInvestMoney())) {
            wrapper.eq("invest_money", vo.getInvestMoney());
        }
        List<FinancialInstitutionInfoEntity> list = baseMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<FinancialInstitutionInfoEntity> getNoCheckList() {
        List<FinancialInstitutionInfoEntity> list=baseMapper.selectList(new QueryWrapper<FinancialInstitutionInfoEntity>().eq("status", StatusEnum.STATU_ONE.getCode()));
        return list;
    }

    @Override
    @Transactional
    public void auth(FinancialInstitutionInfoVo financialInstitutionInfoVo) {
        FinancialInstitutionInfoEntity financialInstitutionInfoEntity=new FinancialInstitutionInfoEntity();

        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(financialInstitutionInfoVo,financialInstitutionInfoEntity);
        BeanUtils.copyProperties(financialInstitutionInfoVo, contactEntity);

        contactEntity.setAddress(financialInstitutionInfoVo.getContactAddress());
        contactEntity.setEmail(financialInstitutionInfoVo.getContactEmail());
        contactEntity.setName(financialInstitutionInfoVo.getContactName());
        contactEntity.setPosition(financialInstitutionInfoVo.getContactPosition());
        contactEntity.setPhone(financialInstitutionInfoVo.getContactPhone());
        Long i = contactService.saveAndCheckExist(contactEntity);
        financialInstitutionInfoEntity.setContactId(i);
        financialInstitutionInfoEntity.setStatus(StatusEnum.STATU_ONE.getCode());
        this.save(financialInstitutionInfoEntity);
        UserEntity user = new UserEntity();
        user.setUserId(financialInstitutionInfoVo.getUserId());
        user.setStatus(1);
        userService.updateById(user);
    }

    @Override
    @Transactional
    public void passCheck(Long financialInstitutionId) {
        FinancialInstitutionInfoEntity financialInstitutionInfoEntity = this.getById(financialInstitutionId);
        financialInstitutionInfoEntity.setStatus(StatusEnum.STATU_TWO.getCode());
        this.updateById(financialInstitutionInfoEntity);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(financialInstitutionInfoEntity.getUserId());
        userEntity.setType(4);
        userEntity.setStatus(2);
        userService.updateById(userEntity);
    }

    @Override
    @Transactional
    public void failCheck(Long financialInstitutionId) {
        FinancialInstitutionInfoEntity financialInstitutionInfoEntity = this.getById(financialInstitutionId);
        financialInstitutionInfoEntity.setStatus(StatusEnum.STATU_THREE.getCode());
        this.updateById(financialInstitutionInfoEntity);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(financialInstitutionInfoEntity.getUserId());
        userEntity.setStatus(0);
        userService.updateById(userEntity);
    }


}